﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.WinKStart
 * 
 * Description: Class to read MenuConfig from Disk
 * 
 * Created:     21.07.2012
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2012 by WMSSoft.net
 * 
 * License:     GPLv3
 * 
 * History:     21.07.2012 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Basic.Xml;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.Const;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;

namespace WMSSoft.Lib.WinKStart
{
    public static class cMenuConfigReader
    {
        static bool m_ConfigLoaded = false;
        static cDataSourcesReader m_DataSources = new cDataSourcesReader();
        static cSearchEnginesReader m_SearchEngines = new cSearchEnginesReader();
        static cFunctionsReader m_Functions = new cFunctionsReader();
        static cStartmenuItem m_SearchResultRootMenuItem = new cStartmenuItem("searchresult_menu_root", "Searchresult Menu Root",
                                                         eLinkExecutionTypes.Program, "",
                                                         eExecutionProcedure.Unknown,
                                                         eItemType.Root, "", null, null, "", "", "", "");
        static System.Xml.XmlDocument m_MenuConfigDoc = null;
        static Dictionary<string, string> m_StringsDictionary = new Dictionary<string, string>();
        static Dictionary<string, cRegion> m_XMLRegions = null;
        static Dictionary<string, cWinKStartContextMenuItem> m_ContextMenu = null;
        static Dictionary<string, CContextMenuTargetItem> _ContextMenuTargets = null;
        static Dictionary<string, cWinKStartAction> m_Actions = null;
        static BitmapImage m_DefaultItemImg = basic_GraphicHelpers.WPFBitmapFromDrawingImage(WMSSoft.Lib.WinKStart.Properties.Resources.Executable);
        static Dictionary<string, Dictionary<string, string>> m_ContextMenuToParent = new Dictionary<string, Dictionary<string, string>>();
        static Dictionary<string, string> m_EventSourceToActionName = new Dictionary<string, string>();
        static Dictionary<int, string> m_ContextMenuIDToName = new Dictionary<int, string>();
        static Dictionary<string, object> m_GuiActionElements = new Dictionary<string, object>();
        static Dictionary<string, Dictionary<string, string>> m_DataSourcesToRegion = new Dictionary<string, Dictionary<string, string>>();

        public static event DataSourceUpdate_EventHandler UpdatedDataSource;

        #region Properties

        public static bool ConfigLoaded { get { return m_ConfigLoaded; } }
        /// <summary>
        /// Contains all DataSource - Names and the Region Name who's used
        /// </summary>
        public static Dictionary<string, Dictionary<string, string>> DataSourcesToRegion
        {
            get { return m_DataSourcesToRegion; }
        }

        public static cFunctionsReader FunctionsReader
        {
            get { return m_Functions; }
        }

        public static Dictionary<string, object> GUIActionElements
        {
            get { return m_GuiActionElements; }
        }

        public static Dictionary<string, string> EventSourceToActionName
        {
            get { return m_EventSourceToActionName; }
        }

        public static Dictionary<string, cWinKStartAction> Actions
        {
            get { return m_Actions; }
        }

        public static Dictionary<string, cWinKStartContextMenuItem> ContextMenu
        {
            get { return m_ContextMenu; }
        }

        public static Dictionary<string, CContextMenuTargetItem> ContextMenuTargets
        {
            get { return _ContextMenuTargets; }
        }

        public static Dictionary<string, Dictionary<string, string>> ContextMenuToParent
        {
            get { return m_ContextMenuToParent; }
        }

        public static Dictionary<int, string> ContextMenuIDToName
        {
            get { return m_ContextMenuIDToName; }
        }

        /// <summary>
        /// Contains all configured DataSources used in WinKStart
        /// </summary>
        public static cDataSourcesReader DataSources
        {
            get { return m_DataSources; }
        }

        /// <summary>
        /// Contains all configured SearchEngines
        /// </summary>        
        public static cSearchEnginesReader SearchEngines
        {
            get { return m_SearchEngines; }
        }

        public static cStartmenuItem RootSearchResult
        {
            get { return m_SearchResultRootMenuItem; }
        }

        /// <summary>
        /// Contains all configured Regions
        /// </summary>
        public static Dictionary<string, cRegion> TabPages
        {
            get { return m_XMLRegions; }
        }

        #endregion

        #region Public_Methods


        /// <summary>
        /// Load Content Configuration-File and return a Object with the settings
        /// </summary>
        /// <param name="Filename">Content configuration File to load</param>
        /// <returns>A CContentConfig Object with Content Configuration</returns>
        public static CContentConfig LoadContentConfig(string Filename)
        {
            try
            {
                if (System.IO.File.Exists(Filename) == true)
                {
                    //an existing instance will be killed to create a new kindly object                   
                    if (m_MenuConfigDoc != null) m_MenuConfigDoc = null;
                    //now load the config - File as XMLDocument
                    m_MenuConfigDoc = new System.Xml.XmlDocument();
                    m_MenuConfigDoc.Load(Filename);

                    System.Xml.XmlNode myRootNode = cXmlHelpers.GetRootNode(m_MenuConfigDoc);

                    cDataSourcesReader tmpDataSourcesConfig = new cDataSourcesReader();
                    cSearchEnginesReader tmpSearchEngines = new cSearchEnginesReader();
                    cFunctionsReader tmpFunctions = new cFunctionsReader();
                    Dictionary<string, cWinKStartContextMenuItem> tmpContextMenu = null;
                    Dictionary<string, cWinKStartAction> tmpActions = null;
                    Dictionary<string, cRegion> tmpRegions = null;
                    Dictionary<string, CContextMenuTargetItem> tmpContextMenuTarget = null;
                    //Load DataSources Config (without Initializing)
                    tmpDataSourcesConfig.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__DATASOURCES), false);
                    tmpSearchEngines.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__SEARCHENGINES)); 
                    tmpContextMenu = ProcessLoad_ContextMenu(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__CONTEXT_MENU));
                    tmpContextMenuTarget = ProcessLoad_ContextMenuTargets(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__CONTEXT_MENU));
                    tmpActions = ProcessLoad_Actions(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__ACTIONS));
                    tmpFunctions.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__FUNCTIONS));
                    tmpRegions = LoadRegions(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__REGIONS), false);
                    
                    CContentConfig tmpContentConfig = new CContentConfig(tmpContextMenu.GetNextMenuID());
                    tmpContentConfig.DataSources = tmpDataSourcesConfig.DatasourceItems.Values.ToList();
                    tmpContentConfig.SearchEngines = tmpSearchEngines.SearchEngines.Values.ToList();
                    tmpContentConfig.ContextMenus = tmpContextMenu.Values.ToList();
                    tmpContentConfig.ContextMenuTargets = tmpContextMenuTarget.Values.ToList();
                    tmpContentConfig.Actions = tmpActions.Values.ToList();
                    tmpContentConfig.Functions = tmpFunctions.Item.Values.ToList();
                    tmpContentConfig.Regions = tmpRegions.Values.ToList();

                    return tmpContentConfig;
                }
                else
                {
                    string tmpFileNotFoundMessage = LanguageStrings.GetText(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUS_XML_NOT_FOUND);
                    tmpFileNotFoundMessage = tmpFileNotFoundMessage.Replace("##FILENAME##", Filename);
                    CMessagelogging.AppendMessage(tmpFileNotFoundMessage, null, true, EMessageTypes.Error);
                    return null;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_LOAD_CONFIG, Ex, true, EMessageTypes.Error);
                return null;
            }
        }

        /// <summary>
        /// Load MenuConfig from File and write settings to cMenuConfigReader - Properties
        /// </summary>
        /// <param name="Filename">Configuration File to load</param>
        public static void LoadMenuConfig(string Filename)
        {
            try
            {
                //first, we test file does exist
                if (System.IO.File.Exists(Filename) == true)
                {
                    //an existing instance will be killed to create a new kindly object                   
                    if (m_MenuConfigDoc != null) m_MenuConfigDoc = null;
                    //now load the config - File as XMLDocument
                    m_MenuConfigDoc = new System.Xml.XmlDocument();
                    m_MenuConfigDoc.Load(Filename);

                    System.Xml.XmlNode myRootNode = cXmlHelpers.GetRootNode(m_MenuConfigDoc);
                    
                    //First load the DataSources
                    m_DataSources.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__DATASOURCES));
                    //Apply DataSources to DataSourcesToRegion - Dictionary
                    ApplyDataSources();
                    //Load Search - Engines
                    m_SearchEngines.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__SEARCHENGINES));                    
                    //Load Contextmenu Items
                    m_ContextMenu = ProcessLoad_ContextMenu(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__CONTEXT_MENU));
                    //Load ContextMenuTargets
                    _ContextMenuTargets = ProcessLoad_ContextMenuTargets(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__CONTEXT_MENU));
                    //Load Actions
                    m_Actions = ProcessLoad_Actions(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__ACTIONS));
                    //Load Functions
                    m_Functions.Load(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__FUNCTIONS));
                    //Load GUI - Elements
                    if (m_XMLRegions != null)
                    {
                        m_XMLRegions.Clear();
                        m_XMLRegions = null;
                    }
                    m_XMLRegions = LoadRegions(cXmlHelpers.GetElementNode(myRootNode, XMLNodeNames.MENUCONFIG_XMLNODE_NAME__REGIONS));

                    m_ConfigLoaded = true;
                }
                else
                {
                    string tmpFileNotFoundMessage = LanguageStrings.GetText(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUS_XML_NOT_FOUND);
                    tmpFileNotFoundMessage = tmpFileNotFoundMessage.Replace("##FILENAME##", Filename);
                    CMessagelogging.AppendMessage(tmpFileNotFoundMessage, null, true, EMessageTypes.Error);
                }
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_LOAD_CONFIG, ex, true, EMessageTypes.Error);
            }
        }

        public static KeyValueAssignment CreateKeyValueAssignmentFromXML(System.Xml.XmlNode Item)
        {
            try
            {
                if (Item != null)
                {
                    string sAssignmentKey = "";
                    string sAssignmentValue = "";

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__KEY] != null)
                    {
                        sAssignmentKey = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__KEY].Value;
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VALUE] != null)
                    {
                        sAssignmentValue = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VALUE].Value;
                    }

                    if (sAssignmentKey.Length > 0 && sAssignmentValue.Length > 0)
                    {
                        return new KeyValueAssignment(sAssignmentKey, sAssignmentValue);
                    }
                    else throw new Exception("Can't create FileExtensionToFilterItem because not all needed attributes filled!");
                } return null;
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't Create FileExtensionToFilter - Item!", Ex);
            }
        }

        /// <summary>
        /// Creates a StartmenuItem from XML - Node
        /// </summary>
        /// <param name="Item"></param>
        public static cStartmenuItem CreateMenuItemFromXML(System.Xml.XmlNode Item)
        {
            try
            {
                if (Item != null)
                {
                    string myViewName = "";
                    string myItemName = "";
                    string myExecutable = "";
                    string myWorkingDirectory = "";
                    string myArguments = "";
                    string myComment = "";
                    string myFont = "Segoe UI";
                    string myFilter = "";
                    string PanelID = "";
                    int myWindowsStyle = 0;
                    int myFontSize = 12;
                    int myItemHeight = 32;
                    Color myFontColor = Color.FromRgb(0, 0, 0);
                    BitmapImage myImage = null;
                    eItemType myItemType = eItemType.None;
                    eExecutionProcedure myExecutionProcedure = eExecutionProcedure.Unknown;
                    eXMLHorizontalAlign myTextAlign = eXMLHorizontalAlign.Left;
                    eXMLVerticalAlign myVerticalAlign = eXMLVerticalAlign.Center;
                    eLinkExecutionTypes myLinkExecutionType = eLinkExecutionTypes.Unknown;

                    //Read the ViewName, this is a must given value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VIEWNAME] != null)
                    {
                        myViewName = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VIEWNAME].Value;
                    }
                    else
                    {
                        CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_VIEWNAME, EMessageTypes.Error);
                        return null;
                    }

                    //Read MenuItem Name, this is a must given value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__NAME] != null)
                    {
                        myItemName = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__NAME].Value;
                    }
                    else
                    {
                        CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_NAME, EMessageTypes.Error);
                        return null;
                    }

                    //Read MenuItem Type, this is a must given value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE] != null)
                    {
                        myItemType = cItemType.GetEnumValueFromString(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE].Value);
                    }
                    else
                    {
                        CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_TYPE, EMessageTypes.Error);
                        return null;
                    }

                    //Read the LinkExecutionType, this is a must given value only if we have a link type
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__LINKEXECUTION_TYPE] != null)
                    {
                        myLinkExecutionType = cLinkExecutionTypes.GetEnumValueFromString(
                            Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__LINKEXECUTION_TYPE].Value);
                    }
                    else
                    {
                        //we write only an exception if here is no label type
                        if (myItemType != eItemType.Label)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_LINK_EXECUTION, EMessageTypes.Error);
                            return null;
                        }
                    }

                    //Read the Executable, this is a must given value if the type is not label and linkexecutiontype is program
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EXECUTABLE] != null)
                    {
                        myExecutable = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EXECUTABLE].Value;
                    }
                    else
                    {
                        //we write only an exception if here is no label type
                        if (myItemType != eItemType.Label && myLinkExecutionType == eLinkExecutionTypes.Program)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_EXECUTION_FILE, EMessageTypes.Error);
                            return null;
                        }
                    }

                    //Read ExecutionProcedure, this is a must given value if the type is not label and linkexecutiontype is code
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EXECUTIONPROCEDURE] != null)
                    {
                        myExecutionProcedure = cExecutionProcedure.GetEnumValueFromString(
                            Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EXECUTIONPROCEDURE].Value);
                        if (myExecutionProcedure == eExecutionProcedure.Unknown)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_EXECUTION_PROCEDURE, EMessageTypes.Error);
                            return null;
                        }
                    }
                    else
                    {
                        //we write only an exception if here is no label type
                        if (myItemType != eItemType.Label && myLinkExecutionType == eLinkExecutionTypes.Code)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_MENUITEM_NO_EXECUTION_PROCEDURE, EMessageTypes.Error);
                            return null;
                        }
                    }

                    //Read WorkingDir, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__WORKINGDIR] != null)
                    {
                        myWorkingDirectory = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__WORKINGDIR].Value;
                    }

                    //Read Arguments, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ARGUMENTS] != null)
                    {
                        myArguments = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ARGUMENTS].Value;
                    }

                    //Read the Image, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__IMAGE] != null)
                    {
                        string myImageSource = System.IO.Path.Combine(cUserSettingsReader.ImagesPath,
                                                                      Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__IMAGE].Value);
                        if (System.IO.File.Exists(myImageSource) == true)
                        {
                            myImage = new BitmapImage(new Uri(myImageSource, UriKind.Absolute));
                        }
                    }
                    else myImage = basic_GraphicHelpers.WPFBitmapFromDrawingImage(WMSSoft.Lib.WinKStart.Properties.Resources.Executable); ;

                    //read WindowStyle, this is a optional value, the default value is 0
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__WINDOWSTYLE] != null)
                    {
                        string myTmpValue = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__WINDOWSTYLE].Value;
                        if (basic_helpers.isNumeric(myTmpValue) == true)
                        {
                            myWindowsStyle = Convert.ToInt32(myTmpValue);
                        }
                        else
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_WINDOWSTYLE,
                                                          EMessageTypes.Information);
                        }
                    }

                    //Read the Font, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONT] != null)
                    {
                        myFont = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONT].Value;
                    }

                    //Read FontSize, this is a optional value, the default is 10
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONTSIZE] != null)
                    {
                        string myTmpValue = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONTSIZE].Value;
                        if (basic_helpers.isNumeric(myTmpValue) == true)
                        {
                            myFontSize = Convert.ToInt32(myTmpValue);
                        }
                        else
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_FONTSIZE,
                                                          EMessageTypes.Information);
                        }
                    }

                    //Read FontColor, this is a optional value, it is a rgb triple
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONTCOLOR] != null)
                    {
                        string myTmpValue = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FONTCOLOR].Value;
                        if (basic_GraphicHelpers.isColorTriple(myTmpValue) == true)
                        {
                            myFontColor = basic_GraphicHelpers.GetColorFromString(myTmpValue);
                        }
                    }

                    //Read ItemHeight, this value is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ITEMHEIGHT] != null)
                    {
                        string myTmpValue = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ITEMHEIGHT].Value;
                        if (basic_helpers.isNumeric(myTmpValue) == true)
                        {
                            myItemHeight = Convert.ToInt32(myTmpValue);
                        }
                        else
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_ITEMHEIGHT,
                                                          EMessageTypes.Information);
                        }
                    }

                    //Read TextAlign, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TEXTALIGN] != null)
                    {
                        eXMLHorizontalAlign myTmpAlign = cXMLHorizontalAlign.GetEnumValueFromString(
                                        Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TEXTALIGN].Value);
                        if (myTmpAlign == eXMLHorizontalAlign.Unknown)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_HORIZONTALALIGN,
                                                          EMessageTypes.Information);
                        }
                        else myTextAlign = myTmpAlign;
                    }

                    //Read VerticalAlign, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VERTICALALIGN] != null)
                    {
                        eXMLVerticalAlign myTmpAlign = cXMLVerticalAlign.GetEnumValueFromString(
                            Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VERTICALALIGN].Value);
                        if (myTmpAlign == eXMLVerticalAlign.Unknown)
                        {
                            CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM_WRONG_VALUE_VERTICALALIGN,
                                                          EMessageTypes.Information);
                        }
                        else myVerticalAlign = myTmpAlign;
                    }

                    //Read Comment, this is a optional value
                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__COMMENT] != null)
                    {
                        myComment = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__COMMENT].Value;
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PANELID] != null)
                    {
                        PanelID = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PANELID].Value;
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER] != null)
                    {
                        myFilter = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER].Value;

                    }

                    //Create a new StartMenuItem - Object with data from xml item
                    cStartmenuItem myNewStartMenuItem = new cStartmenuItem(myItemName, myViewName, myLinkExecutionType,
                                                                           myExecutable, myExecutionProcedure, myItemType,
                                                                           "", myImage, null, null, myArguments,
                                                                           "", myWorkingDirectory, myWindowsStyle,
                                                                           myFont, myFontSize, myFontColor, myItemHeight,
                                                                           myTextAlign, myVerticalAlign, "", "", PanelID, myFilter);
                    return myNewStartMenuItem;
                }
                else return null;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_CREATE_MENUITEM,
                                              ex, true, EMessageTypes.Error);
                return null;
            }
        }

        #endregion Public_Methods

        #region Private_Methods

        static void m_DataSources_UpdatedDataSource(object sender, DataSource e)
        {
            if (UpdatedDataSource != null) UpdatedDataSource(sender, e);
        }

        static void ApplyDataSources()
        {
            try
            {
                Dictionary<string, DataSource>.KeyCollection DSKeys = m_DataSources.DatasourceItems.Keys;
                foreach (string DSKey in DSKeys)
                {
                    if (m_DataSourcesToRegion.ContainsKey(DSKey) == false)
                        m_DataSourcesToRegion.Add(DSKey, new Dictionary<string, string>());
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Could not apply DataSources To DataSource to Region assignment!", Ex);
            }
        }

        /// <summary>
        /// Load all Tabpages from XML configuration file
        /// </summary>
        /// <param name="Regions">XML Node to load from</param>
        static Dictionary<string, cRegion> LoadRegions(System.Xml.XmlNode Regions, bool AddDataSourceToRegion = true)
        {
            try
            {
                if (Regions != null)
                {
                    //m_XMLRegions = null;
                    Dictionary<string, cRegion> tmpRegions = new Dictionary<string, cRegion>();
                    //m_XMLRegions = new Dictionary<string, Classes.MenuConfig.cRegion>();

                    cRegion myCurrentXMLRegion = null;

                    foreach (System.Xml.XmlNode child in Regions.ChildNodes)
                    {
                        myCurrentXMLRegion = null;

                        if (child.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADDREGION)
                        {

                            myCurrentXMLRegion = LoadRegion(child);

                            if (myCurrentXMLRegion != null)
                            {
                                if (AddDataSourceToRegion && myCurrentXMLRegion.DataSource.Length > 0)
                                {
                                    if (m_DataSourcesToRegion.ContainsKey(myCurrentXMLRegion.DataSource) == true)
                                        m_DataSourcesToRegion[myCurrentXMLRegion.DataSource].Add(myCurrentXMLRegion.Name, "");
                                }

                                //Add Region to config                            
                                tmpRegions.Add(myCurrentXMLRegion.Name, myCurrentXMLRegion);
                            }
                        }
                    }
                    return tmpRegions;
                }
                else return new Dictionary<string, cRegion>();

            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_LOAD_TABPAGES_CONFIGURATION, 
                                              ex, true, EMessageTypes.Error);
                return new Dictionary<string, cRegion>();
            }
        }

        /// <summary>
        /// Load an Tabpage from XML File
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        static cRegion LoadRegion(System.Xml.XmlNode Item)
        {
            try
            {
                if (Item != null)
                {
                    cRegion myTabpage = new cRegion();

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__NAME] != null)
                    {
                        myTabpage.Name = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__NAME].Value;
                    }
                    else throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_TABPAGE_NO_NAME);

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION] != null)
                    {
                        myTabpage.Caption = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION].Value;
                    }
                    else throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_TABPAGE_NO_CAPTION);                 

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__SHOW_CONTEXTMENU] != null)
                    {
                        myTabpage.ShowContextMenu = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__SHOW_CONTEXTMENU].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAN_ADD_TO_FAVORITES] != null)
                    {
                        myTabpage.CanAddToFavorite = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAN_ADD_TO_FAVORITES].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAN_REMOVE_FROM_FAVORITES] != null)
                    {
                        myTabpage.CanRemoveFromFavorites = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAN_REMOVE_FROM_FAVORITES].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANNAVIGATE] != null)
                    {
                        myTabpage.CanNavigate = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANNAVIGATE].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__RENDERMETHODE] != null)
                    {
                        myTabpage.RendererMethode = (eRenderType)Enum.Parse(typeof(eRenderType), Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__RENDERMETHODE].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER] != null)
                    {
                        myTabpage.Filter = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER].Value;
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATASOURCE] != null)
                    {
                        myTabpage.DataSource = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATASOURCE].Value;
                    }

                    foreach (System.Xml.XmlNode child in Item.ChildNodes)
                    {

                        if (child.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__PANELS)
                        {
                            ProcessMinimizeablePanels(myTabpage.Panels, child, myTabpage.Name);
                        }

                    }
                    return myTabpage;
                }
                else return null;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_LOAD_TABPAGE_CONFIGURATION, 
                                              ex, true, EMessageTypes.Error);
                return null;
            }
        }

        /// <summary>
        /// Loads the Tabpage Panels Node from XML
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        static void ProcessMinimizeablePanels(MinimizeablePanels Panels, System.Xml.XmlNode Item, string RegionName)
        {
            try
            {
                if (Item != null)
                {
                    MinimizeablePanels myPanels = new MinimizeablePanels();
                    MinimizeablePanel myNewPanel = null;
                    foreach (System.Xml.XmlNode child in Item.ChildNodes)
                    {
                        //reset Field
                        myNewPanel = null;

                        if (child.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADDPANEL)
                        {
                            myNewPanel = ProcessMinimizeablePanel(child);

                            if (myNewPanel != null)
                            {
                                if (myNewPanel.DataSource.Length > 0)
                                {
                                    if (m_DataSourcesToRegion.ContainsKey(myNewPanel.DataSource) == true)
                                    {
                                        if (m_DataSourcesToRegion[myNewPanel.DataSource].ContainsKey(RegionName) == false)
                                            m_DataSourcesToRegion[myNewPanel.DataSource].Add(RegionName, "");
                                    }
                                }
                                Panels.Panels.Add(myNewPanel);
                            }
                        }
                    }
                   
                }               
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_TABPAGE_NODE, 
                                              ex, true, EMessageTypes.Error);     
            }
        }

        /// <summary>
        /// Read the MinimizeablePanel from AddPanel - XML - Node (including subnodes)
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        static MinimizeablePanel ProcessMinimizeablePanel(System.Xml.XmlNode Item)
        {
            try {
                if (Item != null)
                {
                    MinimizeablePanel myPanel = new MinimizeablePanel();

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION] != null)
                    {
                        myPanel.Caption = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION].Value;
                    }
                    else myPanel.Caption = XMLNodeNames.DEFAULT_MINIMIZEABLE_PANEL_ITEM_CAPTION;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__MAX_VIEWED_ELEMENTS] != null)
                    {
                        myPanel.MaxViewedElements = Convert.ToInt32(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__MAX_VIEWED_ELEMENTS].Value);
                    }
                    else myPanel.MaxViewedElements = 5;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PANELID] != null)
                    {
                        myPanel.PanelID = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PANELID].Value;
                    }
                    else myPanel.PanelID = "";

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANCOLLAPSEEXPAND] != null)
                    {
                        myPanel.CanCollapsExpand = Convert.ToBoolean(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANCOLLAPSEEXPAND].Value);
                    }
                    else myPanel.CanCollapsExpand = true;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DEFAULT_OPENED_STYLE] != null)
                    {
                        myPanel.DefaultOpenedPanelStyle = (PanelOpenedStyle)Enum.Parse(typeof(PanelOpenedStyle), 
                                    Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DEFAULT_OPENED_STYLE].Value);
                    }
                    else myPanel.DefaultOpenedPanelStyle = PanelOpenedStyle.Expanded;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CLOSE_IF_OPEN_OTHER_PANEL] != null)
                    {
                        myPanel.CloseIfOpenOtherPanel = 
                            Convert.ToBoolean(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CLOSE_IF_OPEN_OTHER_PANEL].Value);
                    }
                    else myPanel.CloseIfOpenOtherPanel = true;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE] != null)
                    {
                        myPanel.ContentType = (eContentPanelContentType)Enum.Parse(typeof(eContentPanelContentType), 
                            Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE].Value, true);
                    }
                    else myPanel.ContentType = eContentPanelContentType.None;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ITEMTYPE] != null)
                    {
                        myPanel.ItemType = (eItemType)Enum.Parse(typeof(eItemType),
                            Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ITEMTYPE].Value, true);
                    }
                    else myPanel.ItemType = eItemType.Link;

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANNAVIGATE] != null)
                    {
                        //read attribute DataSource
                        myPanel.CanNavigate = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CANNAVIGATE].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__SORTITEMS] != null)
                    {
                        //read attribute DataSource
                        myPanel.SortItems = bool.Parse(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__SORTITEMS].Value);
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATASOURCE] != null)
                    {
                        //read attribute DataSource
                        myPanel.DataSource = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATASOURCE].Value;
                    }

                    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER] != null)
                    {
                        //TODO: read attribute DataSource
                        myPanel.Filter = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FILTER].Value;
                    }

                    return myPanel;
                }
                else return null;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_MINIMIZEABLE_PANEL_NODE, ex, 
                                              true, EMessageTypes.Error);
                return null;
            }

        }

        static Dictionary<string, CContextMenuTargetItem> ProcessLoad_ContextMenuTargets(System.Xml.XmlNode Item)
        {
            try
            {
                Dictionary<string, CContextMenuTargetItem> tmpReturnTargets = new Dictionary<string, CContextMenuTargetItem>();
                CContextMenuTargetItem tmpNewItem = null;
                int iUserContextMenuItemID = 500;

                foreach (System.Xml.XmlNode SubNode in Item.ChildNodes)
                {
                    if (SubNode.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_CONTEXT_MENU_TARGET)
                    {
                        tmpNewItem = null;
                        tmpNewItem = ProcessLoad_ContextMenuTarget(SubNode);

                        if (tmpNewItem != null)
                        {

                            foreach (cWinKStartContextMenuItem CustomItem in tmpNewItem.CustomMenuItems)
                            {
                                if (m_ContextMenuIDToName.ContainsKey(iUserContextMenuItemID) == false)
                                    m_ContextMenuIDToName.Add(iUserContextMenuItemID, CustomItem.Name);
                                CustomItem.MenuID = iUserContextMenuItemID;

                                iUserContextMenuItemID++;
                            }

                            //Add Targetname to Region - List
                            foreach (string Parent in tmpNewItem.UsedIn)
                            {
                                if (m_ContextMenuToParent.ContainsKey(Parent) == false)
                                {
                                    m_ContextMenuToParent.Add(Parent, new Dictionary<string, string>());
                                }

                                if (m_ContextMenuToParent[Parent].ContainsKey(tmpNewItem.Name) == false)
                                    m_ContextMenuToParent[Parent].Add(tmpNewItem.Name, "");
                            }

                            tmpReturnTargets.Add(tmpNewItem.Name, tmpNewItem);
                        }
                    }
                }

                return tmpReturnTargets;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_CONTEXT_MENU_TARGET_ITEMS, Ex);
            }
        }

        static Dictionary<string, cWinKStartContextMenuItem> ProcessLoad_ContextMenu(System.Xml.XmlNode Item)
        {
            try
            {
                m_ContextMenuToParent.Clear();
                m_ContextMenuIDToName.Clear();

                Dictionary<string, cWinKStartContextMenuItem> tmpContextMenus = new Dictionary<string, cWinKStartContextMenuItem>();
                cWinKStartContextMenuItem tmpContextMenuItem = null;
                int iUserContextMenuItemID = 500;

                foreach (System.Xml.XmlNode SubNode in Item.ChildNodes)
                {
                    
                    if (SubNode.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_CONTEXT_MENU_ITEM)
                    {
                        tmpContextMenuItem = null;
                        tmpContextMenuItem = ProcessLoad_ContextMenuItem(SubNode);
                        if (tmpContextMenuItem != null)
                        {
                            //Save MenuId to access the ContextmenuItem from Shell
                            tmpContextMenuItem.MenuID = iUserContextMenuItemID;

                            tmpContextMenus.Add(tmpContextMenuItem.Name, tmpContextMenuItem);
                            //save MenuID to MenuName for later use
                            if (m_ContextMenuIDToName.ContainsKey(iUserContextMenuItemID) == false)
                                m_ContextMenuIDToName.Add(iUserContextMenuItemID, tmpContextMenuItem.Name);
                            
                            foreach (string Parent in tmpContextMenuItem.UsedIn)
                            {
                                if (m_ContextMenuToParent.ContainsKey(Parent) == false)
                                {
                                    m_ContextMenuToParent.Add(Parent, new Dictionary<string, string>());
                                }

                                if (m_ContextMenuToParent[Parent].ContainsKey(tmpContextMenuItem.Name) == false)
                                    m_ContextMenuToParent[Parent].Add(tmpContextMenuItem.Name, "");
                            }
                            iUserContextMenuItemID++;
                        }
                    }
                }

                return tmpContextMenus;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_CONTEXT_MENU_ITEMS, Ex);
            }
        }

        static cWinKStartContextMenuItem ProcessLoad_ContextMenuItem(System.Xml.XmlNode Item)
        {
            try
            {
                cWinKStartContextMenuItem tmpContextMenuItem = new cWinKStartContextMenuItem();

                if (Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME] != null)
                {
                    tmpContextMenuItem.Name = Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_NAME));

                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION] != null)
                {
                    tmpContextMenuItem.Caption = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_CAPTION));

                //Load UsedIn
                System.Xml.XmlNode tmpUsedInNode = cXmlHelpers.GetElementNode(Item, XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__USED_IN_PARENT);
                if (tmpUsedInNode != null)
                {
                    foreach (System.Xml.XmlNode UsedInChild in tmpUsedInNode.ChildNodes)
                    {
                        if (UsedInChild != null)
                        {
                            if (UsedInChild.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_PARENT)
                            {
                                tmpContextMenuItem.UsedIn.Add(UsedInChild.InnerText);
                            }


                        }
                    }
                }
                return tmpContextMenuItem;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_CONTEXT_MENU_ITEM_ATTRIBUTES, Ex);
            }
        }

        static cWinKStartContextMenuItem ProcessLoad_CustomMenuItem(System.Xml.XmlNode Item)
        {
            try
            {
                cWinKStartContextMenuItem tmpContextMenuItem = new cWinKStartContextMenuItem();

                if (Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME] != null)
                {
                    tmpContextMenuItem.Name = Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_NAME));

                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION] != null)
                {
                    tmpContextMenuItem.Caption = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__CAPTION].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_CAPTION));

                return tmpContextMenuItem;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_CONTEXT_MENU_ITEM_ATTRIBUTES, Ex);
            }
        }

        static CContextMenuTargetItem ProcessLoad_ContextMenuTarget(System.Xml.XmlNode Item)
        {
            try
            {
                CContextMenuTargetItem tmpNewContextMenuTargetItem = new CContextMenuTargetItem();
                if (Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME] != null)
                {
                    tmpNewContextMenuTargetItem.Name = Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_NAME));

                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE] != null)
                {
                    tmpNewContextMenuTargetItem.TargetType = (eContextMenuTarget)Enum.Parse(typeof(eContextMenuTarget), Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE].Value);
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_CAPTION));

                //Load UsedIn
                System.Xml.XmlNode tmpUsedInNode = cXmlHelpers.GetElementNode(Item, XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__USED_IN_PARENT);
                if (tmpUsedInNode != null)
                {
                    foreach (System.Xml.XmlNode UsedInChild in tmpUsedInNode.ChildNodes)
                    {
                        if (UsedInChild != null)
                        {
                            if (UsedInChild.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_PARENT)
                            {
                                tmpNewContextMenuTargetItem.UsedIn.Add(UsedInChild.InnerText);
                            }
                        }
                    }
                }

                //Load CustomMenuItems
                System.Xml.XmlNode tmpCustomMenuItemsNode = cXmlHelpers.GetElementNode(Item, XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ITEMS);
                if (tmpUsedInNode != null)
                {
                    cWinKStartContextMenuItem tmpCustomMenuItem = null;
                    foreach (System.Xml.XmlNode CustomMenuItemChild in tmpCustomMenuItemsNode.ChildNodes)
                    {
                        if (CustomMenuItemChild != null)
                        {
                            if (CustomMenuItemChild.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_CONTEXT_MENU_ITEM)
                            {
                                tmpCustomMenuItem = null;
                                tmpCustomMenuItem = ProcessLoad_CustomMenuItem(CustomMenuItemChild);
                                if (tmpCustomMenuItem != null)
                                    tmpNewContextMenuTargetItem.CustomMenuItems.Add(tmpCustomMenuItem);
                            }
                        }
                    }
                }

                return tmpNewContextMenuTargetItem;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_CONTEXT_MENU_TARGET_ITEM, Ex);
            }
        }

        static Dictionary<string, cWinKStartAction> ProcessLoad_Actions(System.Xml.XmlNode Item)
        {
            try
            {
                m_EventSourceToActionName.Clear();

                Dictionary<string, cWinKStartAction> tmpActions = new Dictionary<string, cWinKStartAction>();
                cWinKStartAction tmpAction = null;

                foreach (System.Xml.XmlNode SubNode in Item.ChildNodes)
                {
                    tmpAction = null;

                    if (SubNode.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_ACTION)
                    {
                        tmpAction = ProcessLoad_Action(SubNode);
                        if (tmpAction != null)
                        {
                            tmpActions.Add(tmpAction.Name, tmpAction);
                            if (m_EventSourceToActionName.ContainsKey(tmpAction.EventSourceElement) == false)
                                m_EventSourceToActionName.Add(tmpAction.EventSourceElement, tmpAction.Name);
                        }
                    }
                }

                return tmpActions;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_ACTIONS, Ex);
            }
        }

        static cWinKStartAction ProcessLoad_Action(System.Xml.XmlNode Item)
        {
            try
            {
                cWinKStartAction tmpAction = new cWinKStartAction();
                //load Attribute Name
                if (Item.Attributes[XMLNodeNames.DATASOURCE_XMLNODE_ATTRIBUTE__NAME] != null)
                {
                    tmpAction.Name = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__NAME].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_NAME));
                //load Attribute Type
                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE] != null)
                {
                    tmpAction.Type = (eEventSourceType)Enum.Parse(typeof(eEventSourceType), Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__TYPE].Value);
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_CONTEXTMENU_ITEM_NO_NAME));
                //load Attribute EventSourceElement
                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EVENT_SOURCE_ELEMENT] != null)
                {
                    tmpAction.EventSourceElement = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EVENT_SOURCE_ELEMENT].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_NO_ATTRIBUTE_EVENT_SOURCE_ELEMENT));
                //load Attribute Function
                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FUNCTION] != null)
                {
                    tmpAction.Function = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__FUNCTION].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_NO_ATTRIBUTE_FUNCTION));


                //Read all Trigger
                System.Xml.XmlNode tmpTriggerNode = cXmlHelpers.GetElementNode(Item, XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__TRIGGER);
                Model.MenuConfig.Trigger tmpTrigger = null;

                foreach (System.Xml.XmlNode SubItem in tmpTriggerNode.ChildNodes)
                {
                    if (SubItem.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_TRIGGER)
                    {
                        tmpTrigger = null;
                        tmpTrigger = ProcessLoad_SingleTrigger(SubItem);

                        if (tmpTrigger != null)
                        {
                            tmpAction.Triggers.Add(tmpTrigger.Event.ToString(), tmpTrigger);
                        }
                    }
                }

                //Read all SourceElements
                System.Xml.XmlNode tmpSourceElementsNode = cXmlHelpers.GetElementNode(Item, XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__SOURCE_ELEMENTS);
                SourceElement tmpSourceElement = null;

                foreach (System.Xml.XmlNode SubItem in tmpSourceElementsNode.ChildNodes)
                {
                    if (SubItem.Name == XMLNodeNames.MENUCONFIG_XMLNODE_SUBNODE__ADD_SOURCE_ELEMENTS)
                    {
                        tmpSourceElement = null;
                        tmpSourceElement = ProcessLoad_SourceElement(SubItem);

                        if (tmpSourceElement != null)
                        {
                            tmpAction.SourceElements.Add(tmpSourceElement);
                        }
                    }
                }

                return tmpAction;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_ACTION, Ex);
            }
        }

        static Model.MenuConfig.Trigger ProcessLoad_SingleTrigger(System.Xml.XmlNode Item)
        {
            try
            {
                Model.MenuConfig.Trigger tmpTrigger = new Model.MenuConfig.Trigger();

                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EVENT] != null)
                {
                    tmpTrigger.Event = (eTriggerEvent)Enum.Parse(typeof(eTriggerEvent), Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__EVENT].Value);
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_NO_ATTRIBUTE_EVENT));
                //load Attribute Type
                //if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VALUE] != null)
                //{
                //    tmpTrigger.Value = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__VALUE].Value;
                //}                
                ////load Attribute DataSourceElement
                //if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATALENGTH] != null)
                //{
                //    if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATALENGTH].Value.Length > 0)
                //        tmpTrigger.DataLength = Convert.ToInt32(Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__DATALENGTH].Value);
                //    else
                //        tmpTrigger.DataLength = 0;
                //}
                //else tmpTrigger.DataLength = 0;

                return tmpTrigger;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_ACTION, Ex);
            }
        }

        static SourceElement ProcessLoad_SourceElement(System.Xml.XmlNode Item)
        {
            try
            {
                SourceElement tmpSourceElement = new SourceElement();

                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ELEMENT] != null)
                {
                    tmpSourceElement.Element = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__ELEMENT].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_NO_ATTRIBUTE_ELEMENT));
                //load Attribute Type
                if (Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PROPERTY] != null)
                {
                    tmpSourceElement.Property = Item.Attributes[XMLNodeNames.MENUCONFIG_XMLNODE_ATTRIBUTE__PROPERTY].Value;
                }
                else throw new Exception(LanguageStrings.GetText(ErrorMessages.MENUCONFIG_READER__ERROR_NO_ATTRIBUTE_PROPERTY));

                return tmpSourceElement;
            }
            catch (Exception Ex)
            {
                throw new Exception(WMSSoft.Lib.Texte.ErrorMessages.MENUCONFIG_READER__ERROR_CANT_READ_ACTION_SOURCE_ELEMENTS, Ex);
            }
        }
        #endregion
    }
}
